//===============================================================================
// Microsoft patterns & practices
// Mobile Client Software Factory - July 2006
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Net;
using System.Web.Services.Protocols;
using Microsoft.Practices.Mobile.DisconnectedAgent.Tests.Mocks;
using Microsoft.Practices.Mobile.EndpointCatalog;

namespace Microsoft.Practices.Mobile.DisconnectedAgent.Tests
{
	[TestClass]
	public class WebServiceProxyFactoryFixture
	{
		string defaultURL = @"http://default/url";

		[TestMethod]
		public void WebServiceProxyFactoryCreatesAnObjectOfTheOnlineProxyType()
		{
			Request request = new Request();
			request.Endpoint = "endpoint";
			request.OnlineProxyType = typeof(MockWebServiceProxy);

			string credentialsAccountName = "MyCreds";

			MockEndpointCatalog catalog = new MockEndpointCatalog();
			MockEndpoint endpoint = new MockEndpoint("endpoint");
			endpoint.Default = new MockEndpointConfig(defaultURL);
			endpoint.Default.AccountName = credentialsAccountName;
			catalog.Endpoints.Add("endpoint", endpoint);

			WebServiceProxyFactory factory = new WebServiceProxyFactory((IEndpointCatalog)catalog, new MockCredentialService(null));

			SoapHttpClientProtocol proxy = (SoapHttpClientProtocol)factory.GetOnlineProxy(request, "Network");

			Assert.IsNotNull(proxy);
			Assert.AreEqual(typeof(MockWebServiceProxy), proxy.GetType());
		}

        [TestMethod]
        public void WebServiceProxyFactorySetsCredentials()
        {
            Request request = new Request();
            request.Endpoint = "endpoint";
            request.OnlineProxyType = typeof(MockWebServiceProxy);

            string account = "MyAccount";

            MockEndpointCatalog catalog = new MockEndpointCatalog();
            MockEndpoint endpoint = new MockEndpoint("endpoint");
            endpoint.Default = new MockEndpointConfig(defaultURL);
            endpoint.Default.AccountName = account;
            catalog.Endpoints.Add("endpoint", endpoint);

            NetworkCredential creds = new NetworkCredential("user", "pswd");
            IWSCredentialService credService = new MockCredentialService(creds);

            WebServiceProxyFactory factory = new WebServiceProxyFactory((IEndpointCatalog)catalog, credService);

            SoapHttpClientProtocol proxy = (SoapHttpClientProtocol)factory.GetOnlineProxy(request, "Network");

            Assert.IsNotNull(proxy);
            Assert.AreEqual(creds, proxy.Credentials);
        }

		[TestMethod]
		public void WebServiceProxyFactorySetsAddress()
		{
			Request request = new Request();
			request.Endpoint = "endpoint";
			request.OnlineProxyType = typeof(MockWebServiceProxy);

			MockEndpointCatalog catalog = new MockEndpointCatalog();
			MockEndpoint endpoint = new MockEndpoint("endpoint");
			endpoint.Default = new MockEndpointConfig(defaultURL);
			endpoint.Default.AccountName = "MyAccount";
			catalog.Endpoints.Add("endpoint", endpoint);

            WebServiceProxyFactory factory = new WebServiceProxyFactory((IEndpointCatalog)catalog, new MockCredentialService(null));

			SoapHttpClientProtocol proxy = (SoapHttpClientProtocol)factory.GetOnlineProxy(request, "Network");
			
			Assert.IsNotNull(proxy);
			Assert.AreEqual(defaultURL, proxy.Url);
		}
	}

	[System.Web.Services.WebServiceBindingAttribute(Name = "ServiceSoap", Namespace = "http://tempuri.org/")]
	public class MockWebServiceProxy : SoapHttpClientProtocol
	{
	}
}
